﻿
#include "CreatePlaneDlg.h"
#include <vtkplanesource.h>
#include <vtkActor.h>    
#include <vtkPolyDataMapper.h>    



CreatePlaneDlg::CreatePlaneDlg(QWidget* parent):
    DlgBase(parent)
{
    initUI();
    manageConnections();
}

CreatePlaneDlg::~CreatePlaneDlg()
{
}

bool CreatePlaneDlg::create()
{
    auto pVtkViewWindow = MyVtkViewWindow::GetInstance();
    if(pVtkViewWindow == nullptr)
    {
        return false;
    }

    {
        m_originPointCoord->getValues(m_originPoint[0], m_originPoint[1], m_originPoint[2]);
        m_point1Coord->getValues(m_point1[0], m_point1[1], m_point1[2]);
        m_point2Coord->getValues(m_point2[0], m_point2[1], m_point2[2]);

        //create vtk vtkplane source
        vtkSmartPointer<vtkPlaneSource> planeSource = vtkSmartPointer<vtkPlaneSource>::New();
        planeSource->SetOrigin(m_originPoint);
        planeSource->SetPoint1(m_point1);
        planeSource->SetPoint2(m_point2);
        planeSource->Update();

        vtkSmartPointer<vtkPolyDataMapper> planeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        planeMapper->SetInputConnection(planeSource->GetOutputPort());
        planeMapper->StaticOn();

        vtkSmartPointer<vtkActor> planeActor = vtkSmartPointer<vtkActor>::New();
        planeActor->SetMapper(planeMapper);
        planeActor->GetProperty()->SetColor(QtVtkModle::Global::PlaneColor[0], QtVtkModle::Global::PlaneColor[1], QtVtkModle::Global::PlaneColor[2]);  //set plane color

        //add plane to vtk view
        pVtkViewWindow->GetRenderer()->AddActor(planeActor);
        GetActorManagerInstance()->AddActor(planeActor, PickMode::FACE);
    }

    pVtkViewWindow->GetRenderer()->ResetCamera();
    pVtkViewWindow->GetRenderWindow()->Render();
    return true;
}

void CreatePlaneDlg::initUI()
{
    setWindowTitle("Create Plane");
    QVBoxLayout* pDlgLayout = new QVBoxLayout(this);  //create layout

    {
        m_originPointCoord = new MyCoordinateEdit(this, "Origin Point");
        pDlgLayout->addWidget(m_originPointCoord);

        m_point1Coord = new MyCoordinateEdit(this, " Point 1");
        m_point1Coord->setValues(1, 0, 1);
        pDlgLayout->addWidget(m_point1Coord);

        m_point2Coord = new MyCoordinateEdit(this, " Point 2");
        m_point2Coord->setValues(0, 1, 1);
        pDlgLayout->addWidget(m_point2Coord);
    }

    {
        m_okBtn = new QPushButton("OK", this);
        m_cancelBtn = new QPushButton("Cancel", this);

        QHBoxLayout* okCancelLayout = new QHBoxLayout(this);
        okCancelLayout->addWidget(m_okBtn);
        okCancelLayout->addWidget(m_cancelBtn);
        pDlgLayout->addLayout(okCancelLayout);
    }

    this->setLayout(pDlgLayout);
}

void CreatePlaneDlg::manageConnections()
{
    connect(m_okBtn, &QPushButton::clicked, this, [=]()
            {
                if(create())
                {
                    close();
                }

                QMessageBox::warning(this, "Error", "Failed to create plane!");

            });

    connect(m_cancelBtn, &QPushButton::clicked, this, &CreatePlaneDlg::close);
}



